using System;
using System.Collections.Generic;
using System.Text;
using Wicresoft.SMS.Send.OmsService;

namespace Wicresoft.SMS.Send
{
    class SendSMS
    {
        private static OMSService oms;

        public static string Send(string to, string content)
        {
            try
            {
                if (oms == null)
                    oms = new OMSService();
                oms.Url = ConfigManage.ProviderUrl;
                if (oms.Url == "")
                    return "";
                string xml = BulidSMSData(to, content);
                if (xml != null)
                {
                    string result = oms.SendXms(xml);
                    return result;
                }
                else
                    return "";
            }
            catch (Exception ex)
            {
                Logger.Exception("SendSMS.Send ", ex);
                return ex.Message;
            }
            
        }

        private static string BulidSMSData(string to, string smscontent)
        {
            try
            {
                tXmsData sms = new tXmsData();
                sms.user = new tUser();
                sms.user.userId = ConfigManage.UserName;
                sms.user.password = ConfigManage.Password;
                sms.user.replyPhone = "";

                sms.xmsHead = new tXmsHeader();
                sms.xmsHead.requiredService = "SMS_SENDER";
                sms.xmsHead.sourceType = "other";
                List<string> list = new List<string>();
                foreach (string  var in to.Split(';'))
                {
                    if (var.Trim() != "")
                        list.Add(var);
                }
                sms.xmsHead.to = list.ToArray();

                sms.xmsBody = new tXmsBody();
                sms.xmsBody.format = "SMS";
                tContent content = new tContent();
                content.contentType = "text/plain";
                content.contentId = "";
                content.contentLocation = "";
                content.Value = smscontent;
                sms.xmsBody.content = new tContent[] { content };

                return XmlBuilder.XmlSerializer<tXmsData>(sms);

            }
            catch (Exception ex)
            {
                Logger.Exception("SendSMS.BulidSMSData ", ex);
                return null;
            }
        }

    }
}
